package com.mes.loco.aps.server.controller.aps;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSTriggerPart;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author PengYouWang
 * 2021-11-30 14:53:58
 */
@RestController
@RequestMapping("/api/APSTriggerPart")
public class APSTriggerPartController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(APSTriggerPartController.class);

    final
    APSService wAPSService;

    public APSTriggerPartController(APSService wAPSService) {
        this.wAPSService = wAPSService;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<APSTriggerPart> wServiceResult = wAPSService.APS_QueryTriggerPart(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));

            ServiceResult<List<APSTriggerPart>> wServiceResult = wAPSService.APS_QueryTriggerPartList(wLoginUser, wID,
                    wProductID, wLineID, wCustomerID, wActive);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            98100002, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            if (!wParam.containsKey("data"))
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            APSTriggerPart wAPSTriggerPart = CloneTool.Clone(wParam.get("data"), APSTriggerPart.class);
            if (wAPSTriggerPart == null)
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            ServiceResult<Integer> wServiceResult = wAPSService.APS_UpdateTriggerPart(wLoginUser, wAPSTriggerPart);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量激活或禁用
     */
    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("Active"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            int wActive = StringUtils.parseInt(wParam.get("Active"));

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            // 权限控制
            if (wActive == 1 && Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            98100003, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            if ((wActive == 2 || wActive == 0) && Configuration.readConfigString("EnableAccessControl", "config" +
                    "/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            98100004, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }


            List<APSTriggerPart> wList = CloneTool.CloneArray(wParam.get("data"), APSTriggerPart.class);
            if (wList == null || wList.size() <= 0)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<Integer> wIDList = wList.stream().map(APSTriggerPart::getID).collect(Collectors.toList());

            wAPSService.APS_ActiveTriggerPartList(wLoginUser, wIDList, wActive);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, null);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
